package java_thinking.unit_16;

import java_thinking.net.mindview.util.Generated;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

/**
 * @Desc:   对里练习18中的对象数组进行排序
 * @author: cww
 * @DateTime: 2019/4/10 9:23
 */
class ComparableBerylliumSphere extends BerylliumSphere implements Comparable<BerylliumSphere> {

    public static long getId(BerylliumSphere bs) {
        try {
            Field fid = BerylliumSphere.class.getDeclaredField("id");
            fid.setAccessible(true);
            return fid.getLong(bs);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }
    @Override
    public int compareTo(BerylliumSphere o) {
        long id = getId(this);
        long rvid = getId(o);
        return (id < rvid ? -1 :(id == rvid? 0 :1));
    }
}
public class E21_ArraySort {
    public static void main(String[] args) {
        Random r = new Random(47);
        BerylliumSphere[] a = Generated.array(BerylliumSphere.class,new BSGenerator(),5);
        Collections.shuffle(Arrays.asList(a), r);
        System.out.println("Before sort 1: a = " + Arrays.toString(a));
        try {
            Arrays.sort(a);
            System.out.println("After sort 1: a = " + Arrays.toString(a));
        } catch(ClassCastException e) {
            System.out.println("Array cannot be sorted!");
        }
        for(int i = 0; i < a.length; i++)
            a[i] = new ComparableBerylliumSphere();
        Collections.shuffle(Arrays.asList(a), r);
        System.out.println("Before sort 2: a = " + Arrays.toString(a));
        Arrays.sort(a);
        System.out.println("After sort 2: a = " + Arrays.toString(a));
        Collections.shuffle(Arrays.asList(a), r);
        System.out.println("Before rev. sort 3: a = " + Arrays.toString(a));
        Arrays.sort(a, Collections.reverseOrder());
        System.out.println("After rev. sort 3: a = " + Arrays.toString(a));
    }
}
